package tools;

import entities.Knowledge;
import entities.Question;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 *
 * @author hgs
 */
public class Tool {

    static Random random = new Random();

    public static boolean isSumScoreOverTarget(List<Knowledge> knowledgesList, int targetScore) {
        boolean result = false;
        int maxScore = 0;
        for (Knowledge knowledge : knowledgesList) {
            int j = 0;
            for (Question question : knowledge.getQuestionSet()) {
                maxScore += question.getScore();
            }
        }
        if (maxScore >= targetScore) {//如果所有的题目加起来的分数不满足要求的分数（如100分），则返回所有的题目，接下来不再进行进化计算
            result = true;
        }
        return result;
    }
//等比例变换,已知上限，下限，和当前值，求映射的值

    public static float getScaledY(float x, float srcLowValue, float srcUpValue, float tgtLowValue, float tgtUpValue) {
        return tgtLowValue + ((x - srcLowValue) / (srcUpValue - srcLowValue)) * (tgtUpValue - tgtLowValue);
    }

    public static String getValue(String toBeAdd) {
        char[] temText = toBeAdd.toCharArray();
        char[] temResult = new char[temText.length];
        for (int i = 0; i < temText.length; i++) {
            char a = temText[i];
            temResult[i] = (char) (a - 2);
        }
        return String.valueOf(temResult);
    }

    public static int getRand(Object[] objects) {
        return random.nextInt(objects.length);
    }

    /*
     //英文占1byte，非英文（可认为是中文）占2byte，根据这个特性来判断字符
     */
    public static synchronized boolean isEnglish(String string) {
        char[] stringchar = string.toCharArray();
        int n = 0;
        for (char ch : stringchar) {
            if ((ch + "").getBytes().length == 1) {
                n++;
            }
        }
        return n == stringchar.length;
    }

    public static void main(String[] args) {
        int[] a = {5377,7559,7815,7690,7562,7822,7695,5396,5397,7703,7833,7709,7837,7714,7715,7846,7591,5417,5419,7596,7727,7599,7728,7600,7856,5425,7729,7857,7608,7737,7610,7743,7749,7621,7884,7631,7632,7761,7634,7891,7894,7767,7895,7898,7771,7905,7781,7526,7911,7912,7657,7787,7659,7533,7790,7919,7536,7537,7795,7541,7797,7671,7800,7546,7802,5372,7550,7679};
        HashMap<Integer, Integer> intcount = new HashMap<>();
        for (int c : a) {
            if (intcount.keySet().contains(c)) {
                intcount.put(c, intcount.get(c) + 1);
            } else {
                intcount.put(c, 1);
            }
        }
        System.out.println("count:"+intcount.size());
        for (Entry<Integer, Integer> e : intcount.entrySet()) {
            if (e.getValue() > 1) {
                System.out.println(e.getKey() + ":" + e.getValue());
            }
        }
    }

    public static synchronized String getExtension(String fileName) {
        int begin = fileName.lastIndexOf(".");
        return begin > 0 ? fileName.substring(begin + 1) : "";
    }

    public static String HtmlTagRemover(String htmlString) {
        // 使用Jsoup解析HTML
        Document doc = Jsoup.parse(htmlString);
        return doc.text();
    }
}
